﻿using System;
using System.Collections.Generic;
using System.Data;

namespace ObjectManager
{
    public class BoMon : ManagerComponent
    {
        private List<Lop> _lop = new List<Lop>();

        public List<Lop> Lop { get; set; }

        private BoMonDAO BoMonDAO { get; set; }

        public BoMonDTO ThongTin { get; set; }

        private void Initialize()
        {
            BoMonDAO = new BoMonDAO();
            ThongTin = new BoMonDTO();
            Lop = new List<Lop>();
        }

        public BoMon()
        {
            Initialize();
        }

        public BoMon(string Ten)
        {
            Initialize();
            ThongTin.Ten = Ten;
        }

        public BoMon(string Ten, string KyHieu)
        {
            Initialize();
            ThongTin.Ten = Ten;
            ThongTin.KyHieu = KyHieu;
        }

        public override int Add()
        {
            BoMonDAO.BoMonDTO = ThongTin;
            int check = BoMonDAO.Insert();
            if (check > 0)
            {
                foreach (Lop item in this.Lop)
                {
                    item.ThongTin.BoMon = ThongTin.Ten;
                    check += item.Add();
                }
                return check;
            }
            return -1;
        }

        public override int Update()
        {
            BoMonDAO.BoMonDTO = ThongTin;
            return BoMonDAO.Update();
        }

        public override int Remove()
        {
            BoMonDAO.BoMonDTO = ThongTin;
            return BoMonDAO.Delete();
        }

        public List<string> ListName()
        {
            return BoMonDAO.ListName();
        }

        public DataTable ListAll()
        {
            return BoMonDAO.ListAll();
        }

        public DataTable ListAll(string Ma)
        {
            int ma = 0;
            if (!string.IsNullOrEmpty(Ma))
            {
                ma = int.Parse(Ma);
            }
            return BoMonDAO.ListAll(ma);
        }

        public DataTable ListAll(string Ten, string KyHieuBM)
        {
            if (string.IsNullOrEmpty(Ten))
            {
                Ten = "%";
            }
            else
            {
                Ten = "%" + Ten + "%";
            }
            if (string.IsNullOrEmpty(KyHieuBM))
            {
                KyHieuBM = "%";
            }
            else
            {
                KyHieuBM = "%" + KyHieuBM + "%";
            }
            return BoMonDAO.ListAll(Ten, KyHieuBM);
        }

        public int Ma()
        {
            int result = 0;
            DataTable temp = BoMonDAO.Ma();
            if (temp != null && temp.Rows.Count > 0)
            {
                result = int.Parse(temp.Rows[0][0].ToString());
            }
            return result;
        }
    }
}
